import { Component, OnInit, AfterViewInit, OnDestroy, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { NzModalService } from 'ng-zorro-antd/modal';
import { OrganizationService } from 'src/app/services/organization.service';
import { NzMessageService } from 'ng-zorro-antd/message';
import { PageTitleService } from 'src/app/services/page-title.service';
import { ExceptionHandler } from 'src/app/handler/exception.handler';
import { PlatformLocation } from '@angular/common';

interface ItemData {
  name: string;
  age: number;
  address: string;
}
@Component({
  selector: 'zepride-organization',
  templateUrl: './organization.component.html',
  styleUrls: ['./organization.component.less']
})

export class OrganizationComponent implements OnInit {
  listOfData1: ItemData[] = [];
  listOfData = [];
  total = 1;
  loading = true;
  params = {
    name: '',
    status: '',
    pageIndex: 1,
    countOfPage: 10
  }
  flag = false
  constructor(
    private route: Router,
    private modal: NzModalService,
    private organizationService: OrganizationService,
    private msg: NzMessageService,
    private phSvr: PageTitleService,
    private locations: PlatformLocation,
  ) {
    this.phSvr.setPageHeaderOption({
      title: '组织机构查询',
      subTitle: '用于查询系统中组织机构列表信息',
      buttons: [
        {
          text: '添加组织机构',
          ngType: 'primary',
          icon: "plus-circle",
          onClick: () => {
            this.addEditData(0);
          }
        },
      ]
    });
  }
  ngOnInit(): void {
    this.organizationList();
    if (this.locations['location'].pathname == "/main/orgList") {
      this.flag = true
    }
    for (let i = 0; i < 100; i++) {
      this.listOfData1.push({
        name: `Edward King ${i}`,
        age: 32,
        address: `London`
      });
    }

    console.info();
  }
  async organizationList() {
    this.loading = true;
    try {
      const data = await this.organizationService.organizationList(this.params);
      this.listOfData = data.list;
      this.total = data.total;
      this.loading = false;
      console.log(this.total);

    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }

  changePageIndex(pageIndex) {
    this.params.pageIndex = pageIndex;
    this.organizationList();
  }
  changePageSize(pageSize) {
    this.params.countOfPage = pageSize;
    this.organizationList();
  }
  clickStatus(item) {
    this.modal.confirm({
      nzTitle: `您确定要${item.status == 0 ? '禁用' : '启用'}么?`,
      nzOnOk: () => {
        this.updateOrganization(item);
      }
    });
  }
  async updateOrganization(item) {
    let params = {
      id: item.id,
      code: item.code,
      creditCode: item.creditCode,
      name: item.name,
      status: item.status == 0 ? '1' : '0'
    }
    try {
      await this.organizationService.updateOrganization(params);
      item.status = item.status == 0 ? 1 : 0;
      this.msg.success(`${item.status == 0 ? '启用' : '禁用'}成功`);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }
  delListItem(id, name, i) {
    this.modal.confirm({
      nzTitle: `您确定要删除<b style="color: red;">${name}</b>么?`,
      nzOnOk: () => {
        this.deleteOrganization(id, i);
      }
    });
  }
  async deleteOrganization(id, i) {
    try {
      await this.organizationService.deleteOrganization(id);
      this.msg.success('删除成功');
      this.listOfData.splice(i, 1);
    } catch (error) {
      ExceptionHandler.current.handleError(error);
    }
  }
  addEditData(Id) {
    this.route.navigate(['/main/editorganization'], { queryParams: { Id: Id } });
  }
  detailsInfo(Id) {
    this.route.navigate(['/main/orgInfo'], { queryParams: { Id: Id } });
  }
  clickPreview(Id) {
    window.open(`http://localhost:4202/login?orgId=${Id}`, '_blank');
  }
  configSystem(orgItem) {
    console.log(orgItem);
    localStorage.setItem('orgid', orgItem.id);
    this.route.navigate(['/main/config']);
  }


  clear() {
    this.params.name = '';
    this.onSearch();
  }
  onSearch() {
    this.params.pageIndex = 1;
    this.params.status = this.params.status ? this.params.status : '';
    this.organizationList();
  }

}
